/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年1月4日         luwei
 */

package com.coocaa.launcher;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;

import com.coocaa.launcher.framework.sidebar.item.ItemFactory.ITEM;
import com.coocaa.launcher.settings.defaultsetting.DefaultLauncherSetting;
import com.coocaa.launcher.sidebar.setting.DeskSettingLayout;
import com.coocaa.launcher.sidebar.setting.SettingLayout;
import com.coocaa.launcher.sidebar.setting.layout.about.AboutLayout;
import com.coocaa.launcher.sidebar.setting.layout.deviceinfo.DeviceInfoLayout;
import com.coocaa.launcher.sidebar.setting.layout.qrcode.QrcodeLayout;
import com.coocaa.launcher.wallpaper.WallpaperManager;

public class SettingActivity extends LauncherBaseActivity {
    private SettingLayout mLayout = null;
    private DefaultLauncherSetting mDefaultSetting = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Uri uri = getIntent().getData();
        WallpaperManager.getInstance().setChangeSuccess(false);
        if (uri != null) {
            String type = uri.getQueryParameter("value");
            if (type != null) {
                if (type.equals(ITEM.DEVICE_INFO.toString())) {
                    mLayout = new DeviceInfoLayout(this);
                } else if (type.equals(ITEM.SETTING.toString()) || type.equals(ITEM.SYS_SETTING.toString())) {
                    mLayout = new DeskSettingLayout(this, type);
                    mDefaultSetting = DefaultLauncherSetting.getInstance(this);
                } else if (type.equals(ITEM.ABOUT_US.toString())) {
                    mLayout = new AboutLayout(this);
                } else if (type.equals(ITEM.MULTI_SCREEN.toString())) {
                    mLayout = new QrcodeLayout(this);
                }
            } else {
                mLayout = new DeskSettingLayout(this, ITEM.SETTING.toString());
            }
        }
        setContentView(mLayout);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (WallpaperManager.getInstance().getChangeSuccess())
            finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLayout.destory();
        if (mDefaultSetting != null) {
            mDefaultSetting.destory();
        }
    }

    public static String getVersion(Context context) {
        PackageManager packageManager = context.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        String version = "";
        PackageInfo packInfo = null;
        ;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            version = packInfo.versionName;
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return version;
    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return "电视派桌面设置页面";
    }
}
